package system

import (
	"context"
	"gf-vben-admin/consts"
	model "gf-vben-admin/internal/model/system"
	"gf-vben-admin/internal/model/system/entity"
	"gf-vben-admin/internal/service/system/internal/dao"
	utils "gf-vben-admin/utility"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
)

type (
	sRolePermission struct{}
)

var (
	insRolePermission = sRolePermission{}
)

func RolePermission() *sRolePermission {
	return &insRolePermission
}

func (s *sRolePermission) PageList(ctx context.Context, param model.RolePermission) (total int, result []*entity.RolePermission, err error) {
	result = make([]*entity.RolePermission, 0)
	daoModel := dao.RolePermission.Ctx(ctx)
	columnMap := dao.RolePermission.ColumnMap()
	daoModel, err = utils.GetWrapper(param, daoModel, columnMap)
	if err != nil {
		return
	}
	total, err = daoModel.Count()
	if err != nil {
		g.Log().Error(ctx, err)
		err = gerror.New("获取总行数失败")
		return
	}
	if total == 0 {
		return
	}
	err = daoModel.Page(param.PageNum, param.PageSize).Scan(&result)
	if err != nil {
		g.Log().Error(ctx, err)
		err = gerror.New("获取数据失败")
	}
	return
}

func (s *sRolePermission) List(ctx context.Context, param model.RolePermission) (result []*entity.RolePermission, err error) {
	result = make([]*entity.RolePermission, 0)
	daoModel := dao.RolePermission.Ctx(ctx)
	columnMap := dao.RolePermission.ColumnMap()
	daoModel, err = utils.GetWrapper(param, daoModel, columnMap)
	if err != nil {
		return
	}
	daoModel, err = utils.GetWrapper(param, daoModel, columnMap)
	if err != nil {
		return
	}
	err = daoModel.Scan(&result)
	if err != nil {
		g.Log().Error(ctx, err)
		err = gerror.New("获取数据失败")
	}
	return
}

func (s *sRolePermission) Add(ctx context.Context, in model.RolePermission) (err error) {
	err = dao.RolePermission.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
		_, err := tx.Model(dao.RolePermission.Table()).Ctx(ctx).OmitEmpty().Data(in).Insert()
		return err
	})
	return
}

func (s *sRolePermission) Edit(ctx context.Context, in model.RolePermission) (err error) {
	err = dao.RolePermission.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
		_, err := tx.Model(dao.RolePermission.Table()).Ctx(ctx).OmitEmpty().Data(in).Where(g.Map{
			dao.RolePermission.Columns().Id: in.Id,
			consts.VersionNumberColumn:      in.VersionNumber,
		}).Update()
		return err
	})
	return
}

func (s *sRolePermission) EditState(ctx context.Context, ids []int64, state int8) (err error) {
	err = dao.RolePermission.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
		_, err := tx.Model(dao.RolePermission.Table()).Ctx(ctx).Data(g.Map{consts.StateColumn: state}).Where(g.Map{
			dao.RolePermission.Columns().Id: ids,
		}).Update()
		return err
	})
	return
}

func (s *sRolePermission) Delete(ctx context.Context, ids []int64) (err error) {
	err = dao.RolePermission.Ctx(ctx).Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
		_, err := tx.Model(dao.RolePermission.Table()).Ctx(ctx).Where(g.Map{
			dao.RolePermission.Columns().Id: ids,
		}).Delete()
		return err
	})
	return
}
